//
//  ByViewShopHomeIndexTable.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/31.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewShopIndexTable.h"

// vc
#import "ByShopIndexPartVC.h"

// cell
#import "ByCellShopIndexFixedPart.h"    // 固定板块
#import "ByCellShopIndexFlexiblePart.h" // 灵活板块
#import "ByCellShopProduct.h"           // 商品


@interface ByViewShopIndexTable() <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView       *table;
@property (nonatomic, strong) ByViewCarousel    *carousel;

@end

@implementation ByViewShopIndexTable
{
    NSInteger _items;
}


- (void)setFrame:(CGRect)frame {
    
    [super setFrame:frame];
    
    self.table = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.backgroundColor = BY_BG_COLOR;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self addSubview:self.table];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    // 注册
    [self.table registerClass:[ByCellShopIndexFixedPart class] forCellReuseIdentifier:@"fixedPartCell"];
    [self.table registerClass:[ByCellShopIndexFlexiblePart class] forCellReuseIdentifier:@"flexiblePartCell"];
    [self.table registerClass:[ByCellShopProduct class] forCellReuseIdentifier:@"productCell"];

    self.carousel = [[ByViewCarousel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 2) timerDuration:3];
    self.carousel.backgroundColor = BY_BLUE_COLOR;
    self.carousel.imageURLStringDatas = @[@"", @""];
    self.table.tableHeaderView = self.carousel;
    
    __weak typeof(self) wself = self;
    self.contentViewDidAppear = ^(NSInteger index) {
        
        _items = arc4random_uniform(5) + 1;
        [wself.table reloadData];
    };
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    // 商品
    if (section == 2) {
        return 3;
    }
    
    
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        ByCellShopIndexFixedPart *cell = [tableView dequeueReusableCellWithIdentifier:@"fixedPartCell"];
        return cell;
    }
    else if (indexPath.section == 1) {
        ByCellShopIndexFlexiblePart *cell = [tableView dequeueReusableCellWithIdentifier:@"flexiblePartCell"];
        cell.parts = @[@"后台板块1", @"后台板块2"];
        return cell;
    }
    else if (indexPath.section == 2) {
        ByCellShopProduct *cell = [tableView dequeueReusableCellWithIdentifier:@"productCell"];
        return cell;
    }
    
    
    return UITableViewCell.new;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return [tableView fd_heightForCellWithIdentifier:@"fixedPartCell" cacheByIndexPath:indexPath configuration:^(id cell) {
            // Do Nothing
        }];
    }
    else if (indexPath.section == 1) {
        return [tableView fd_heightForCellWithIdentifier:@"flexiblePartCell" cacheByIndexPath:indexPath configuration:^(ByCellShopIndexFlexiblePart *cell) {
            cell.parts = @[@"后台板块1", @"后台板块2"];
        }];
    }
    // 商品
    else if (indexPath.section == 2) {
        return (SCREEN_WIDTH - 20) / 2 + 40;
    }
    
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    // 商品
    if (section == 2) {
        return 40;
    }
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    // 商品
    if (section == 2) {
        UIView *header = [UIView new];
        
        // 线条
        UIView *line = [UIView new];
        line.backgroundColor = BY_RED_COLOR;
        [header addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(header).multipliedBy(0.7);
            make.centerX.equalTo(@0);
            make.centerY.equalTo(@0);
            make.height.equalTo(@1);
        }];     
        
        UILabel *label = [[UILabel alloc] init];
        label.backgroundColor = tableView.backgroundColor;
        label.text = @"最近的“蜜蜂”";
        label.font = FONT(14);
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = BY_RED_COLOR;
        [header addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@100);
            make.top.bottom.equalTo(@0);
            make.centerX.equalTo(@0);
        }];
        
        return header;
    }
    return UIView.new;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return UIView.new;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:false];
    
    
    [((ByBaseVC *)self.viewController) by_normalPush:[ByShopIndexPartVC new] hasNavi:true];
}

@end
